package com.example.scity.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextClock;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.bumptech.glide.Glide;
import com.example.scity.APIInterface;
import com.example.scity.Configs;
import com.example.scity.DdlbActivity;
import com.example.scity.GrzxActivity;
import com.example.scity.HouseActivity;
import com.example.scity.HouseDetailActivity;
import com.example.scity.LoginActivity;
import com.example.scity.R;
import com.example.scity.XgmmActivity;
import com.example.scity.YjfkActivity;
import com.example.scity.beens.GrzxBeen;
import com.example.scity.beens.MessageBeen;
import com.google.gson.Gson;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class Fragment5 extends Fragment {
    View view;
    Button button;
    TextView txtGrxx;
    TextView txtDdlf;
    TextView txtXgmm;
    TextView txtYjfk;
    TextView txtUsername;
    TextView txttuichu;
    GrzxBeen grzxBeen;
    MessageBeen messageBeen;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    ImageView meimageView;
                    meimageView = view.findViewById(R.id.me_imageView);
                    Glide.with(getActivity()).load(Configs.Host + messageBeen.user.avatar).into(meimageView);
                    TextView mename;
                    mename = view.findViewById(R.id.me_imageView);
                    mename.setText(messageBeen.user.userName);
                    break;
                case 1:
                    Toast.makeText(getActivity(), "退出成功", Toast.LENGTH_LONG).show();
                    Configs.Token = "";
                    break;
            }
        }
    };
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_5,container,false);

        initGrxx();//个人信息
        initDdlb();//订单列表
        initXgmm();//修改密码
        initYjfk();//意见反馈
        initButton();//登录
        tuichu();
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        txtUsername=view.findViewById(R.id.me_yhm);
        txtUsername.setText("112");
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder().url(Configs.Host+ APIInterface.GRZX).addHeader(Configs.AUKEY,Configs.Token).get().build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {

            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                grzxBeen = new Gson().fromJson(response.body().string(), GrzxBeen.class);
               // handler.sendEmptyMessage(0);
            }
        });
    }
    private void tuichu() {
        txttuichu=view.findViewById(R.id.me_tuichu);
        txttuichu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                OkHttpClient okHttpClient = new OkHttpClient();
                MediaType mediaType = MediaType.parse("application/json");
                RequestBody requestBody = RequestBody.create(mediaType, "");
                Request request = new Request.Builder()
                        .url(Configs.Host + APIInterface.logout)
                        .addHeader("Authorization", Configs.Token)
                        .post(requestBody)
                        .build();
                okHttpClient.newCall(request).enqueue(new Callback() {
                    @Override
                    public void onFailure(@NonNull Call call, @NonNull IOException e) {

                    }

                    @Override
                    public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                        handler.sendEmptyMessage(1);
                    }
                });
            }
        });
    }
    //意见反馈
    private void initYjfk() {
        txtYjfk=view.findViewById(R.id.me_yjfk);
        txtYjfk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(), YjfkActivity.class));
            }
        });
    }
    //修改密码
    private void initXgmm() {
        txtXgmm=view.findViewById(R.id.me_xgmm);
        txtXgmm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(), XgmmActivity.class));
            }
        });
    }
    //订单列表
    private void initDdlb() {
        txtDdlf=view.findViewById(R.id.me_ddlb);
        txtDdlf.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(Configs.Token==""){
                    startActivity(new Intent(getActivity(),LoginActivity.class));
                }else {
                    startActivity(new Intent(getActivity(), DdlbActivity.class));
                }
            }
        });
    }
    //个人信息
    private void initGrxx() {
        txtGrxx=view.findViewById(R.id.me_grxx);
        txtGrxx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(Configs.Token==""){
                    startActivity(new Intent(getActivity(),LoginActivity.class));
                }else {
                    startActivity(new Intent(getActivity(), GrzxActivity.class));
                }
            }
        });
    }

    private void initButton() {
        button = view.findViewById(R.id.meloadbtn);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), LoginActivity.class));
            }
        });
    }
}
